﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TRPOP.Common;

namespace TRPOP.CMS.Model
{
    /// <summary>
    /// 系统基本信息
    /// </summary>
    [Serializable]
    public class SystemInfo
    {
        #region 私有变量
        private string m_SystemName = "TRPOP_CMS管理系统";
        private string m_RootNodeName = "根结点";
        private string m_CMSAdminPageSuffixName = "TRPOP_CMS管理系统";
        private string m_DBprefixTableName = "cms";
        private string m_AdminFolderName = "cms";
        private string m_CMSDomian = "localhost";
        private string m_CMSRoot = "/";
        private string m_PagerStoreProcedureName = "DataBasePager";
        private string m_CMSDataBaseConnection = @"server=.;database=TRPOP_CMS;uid=sa;pwd=123456;pooling=true;Min Pool Size=50;Max Pool Size=100;Connect Timeout=60";
        private string m_DBOwner = "dbo";
        private string m_NameSpaceForTemplateDal = "TRPOP.CMS.TemplateSQLServerDAL";
        private string m_NameSpaceForTemplateFunc = "TRPOP.CMS.Template";
        private string m_NameSpaceForCMSDAL = "TRPOP.CMS.DALSQLServer";
        private string m_CMSThemes = "Classic";
        #endregion

        #region 成员函数

        #region 常量名称
        /// <summary>
        /// 系统名称，默认： TRPOP_CMS管理系统
        /// </summary>
        public string SystemName
        {
            get { return m_SystemName; }
            set { m_SystemName = value; }
        }

        /// <summary>
        /// 根节点名称
        /// </summary>
        public string RootNodeName
        {
            get { return m_RootNodeName; }
            set { m_RootNodeName = value; }
        }

        /// <summary>
        /// CMS后台主题
        /// </summary>
        public string CMSThemes
        {
            get
            {
                return m_CMSThemes;
            }
            set
            {
                m_CMSThemes = value;
            }
        }
        #endregion

        #region 基本参数

        /// <summary>
        /// CMS后台页面 后缀名
        /// </summary>
        public string CMSAdminPageSuffixName
        {
            get { return m_CMSAdminPageSuffixName; }
            set { m_CMSAdminPageSuffixName = value; }
        }

        /// <summary>
        /// 系统表前缀，默认： CMS
        /// </summary>
        public string DBprefixTableName
        {
            get { return m_DBprefixTableName; }
            set { m_DBprefixTableName = value; }
        }

        /// <summary>
        /// CMS管理后台所在的目录名，默认： CMS
        /// </summary>
        public string AdminFolderName
        {
            get { return m_AdminFolderName; }
            set { m_AdminFolderName = value; }
        }

        /// <summary>
        /// CMS管理平台所在的域名，默认： localhost
        /// </summary>
        public string CMSDomian
        {
            get { return m_CMSDomian; }
            set { m_CMSDomian = value; }
        }

        /// <summary>
        /// CMS管理平台所在的目录(全路径)，默认： /
        /// </summary>
        public string CMSRoot
        {
            get { return m_CMSRoot; }
            set { m_CMSRoot = value; }
        }

        /// <summary>
        /// CMS数据库连接字符串
        /// </summary>
        public string CMSDataBaseConnection
        {
            get { return m_CMSDataBaseConnection; }
            set { m_CMSDataBaseConnection = value; }
        }

        /// <summary>
        /// 数据库的持有者
        /// </summary>
        public string DBOwner
        {
            get { return m_DBOwner; }
            set { m_DBOwner = value; }
        }

        /// <summary>
        /// 分页存储过程名称
        /// </summary>
        public string PagerStoreProcedureName
        {
            get { return m_PagerStoreProcedureName; }
            set { m_PagerStoreProcedureName = value; }
        }
        #endregion

        #region NameSpace
        /// <summary>
        /// CMS数据处理操作类，默认：TRPOP.CMS.DALSQLServer
        /// </summary>
        public string NameSpaceForCMSDAL
        {
            get { return m_NameSpaceForCMSDAL; }
            set { m_NameSpaceForCMSDAL = value; }
        }

        /// <summary>
        /// 模板的 数据处理 命名空间
        /// </summary>
        public string NameSpaceForTemplateDal
        {
            get { return m_NameSpaceForTemplateDal; }
            set { m_NameSpaceForTemplateDal = value; }
        }

        /// <summary>
        /// 模板处理的 命名空间
        /// </summary>
        public string NameSpaceForTemplateFunc
        {
            get { return m_NameSpaceForTemplateFunc; }
            set { m_NameSpaceForTemplateFunc = value; }
        }

        /// <summary>
        /// 模板的 数据处理 为模板实现数据访问的类名
        /// </summary>
        public string ClassNameForTemplateDal
        {
            get { return NameSpaceForTemplateDal + ".TemplateDataManage"; }
        }
        /// <summary>
        /// 模板处理的 函数处理类名
        /// </summary>
        public string ClassNameForTemplateFunc
        {
            get { return NameSpaceForTemplateFunc + ".TemplateFuncInstance"; }
        }
        #endregion

        #endregion
    }

    /// <summary>
    /// 获取当前的系统基本信息
    /// </summary>
    public static class SystemInfoHelper
    {
        private static SystemInfo mSystemInfo;

        /// <summary>
        /// 获取系统信息
        /// </summary>
        /// <returns></returns>
        public static SystemInfo getSystemInfo()
        {
            if (mSystemInfo == null)
            {
                setSystemInfo();
            }
            return mSystemInfo;
        }

        /// <summary>
        /// 设置系统信息
        /// </summary>
        private static void setSystemInfo()
        {
            string strFileName = Common.Util.GetMapPath(SystemConst.SystemInfoFileName);
            object obj = null;
            if (FileUtil.IsExist(strFileName))
            {
                try
                {
                    obj = SerializationHelper.Load(typeof(SystemInfo), strFileName);
                }
                catch
                {
                    obj = null;
                }
                finally
                {
                    if (obj is SystemInfo == false || obj == null)
                    {
                        obj = new SystemInfo();
                        SerializationHelper.Save(obj, strFileName);
                    }
                    mSystemInfo = obj as SystemInfo;
                }
            }
            else
            {
                mSystemInfo = new SystemInfo();
                SerializationHelper.Save(mSystemInfo, strFileName);
            }
        }

        /// <summary>
        /// 重置系统信息
        /// </summary>
        public static void ReSetSystemInfo()
        {
            mSystemInfo = null;
        }
    }
}
